import tkinter as tk
from tkinter import messagebox
from typing import Callable


class LoginView:
    # 登录/注册界面

    def __init__(self, master: tk.Tk):
        self.master = master
        self.frame = tk.Frame(master)
        self._setup_ui()

    def _setup_ui(self):
        # 初始化界面组件
        self.frame.pack(pady=50)

        # 用户名输入
        tk.Label(self.frame, text="用户名:").grid(row=0, column=0, pady=5)
        self.username_entry = tk.Entry(self.frame)
        self.username_entry.grid(row=0, column=1, pady=5)

        # 密码输入
        tk.Label(self.frame, text="密码:").grid(row=1, column=0, pady=5)
        self.password_entry = tk.Entry(self.frame, show="*")
        self.password_entry.grid(row=1, column=1, pady=5)

        # 按钮区域
        btn_frame = tk.Frame(self.frame)
        btn_frame.grid(row=2, columnspan=2, pady=10)

        self.login_btn = tk.Button(btn_frame, text="登录")
        self.login_btn.pack(side=tk.LEFT, padx=5)

        self.register_btn = tk.Button(btn_frame, text="注册")
        self.register_btn.pack(side=tk.LEFT, padx=5)

    def bind_commands(self, login_cmd: Callable, register_cmd: Callable):
        # 绑定按钮事件
        self.login_btn.config(command=login_cmd)
        self.register_btn.config(command=register_cmd)

    def get_credentials(self) -> tuple[str, str]:
        # 获取输入的用户名和密码
        return (
            self.username_entry.get(),
            self.password_entry.get()
        )

    def show_error(self, message: str):
        # 显示错误信息
        messagebox.showerror("错误", message)

    def destroy(self):
        # 销毁界面
        self.frame.destroy()